package com.org.javabody.ReflectApi;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ReflectDemo {

	public ReflectDemo() {
		
	}

	public ReflectDemo(String str) {
		System.out.println(str);
	}
	public int add() {
		int a =1;
		int b =1;
		return a+b;
	}
	public static void main(String[] args) throws Exception {
		Class<?> cl = Class.forName("ReflectApi.ReflectDemo");
		Object object = cl.newInstance();  //获取无参构造

		Method addMethod = cl.getMethod("add", new Class[] {});//得到ReflectDemo类的add方法

		Object result = addMethod.invoke(object, new Object[] {});
		System.out.println(result);
		//-----------------------------------------------

		Constructor cons2 = cl.getConstructor(new Class[] {String.class}); //获取有参构造

	    Object obj2 = cons2.newInstance(new Object[] {"调用有参构造"});
	}

}
